package cn.com.huzo.repository.tech.impl;

import cn.com.huzo.model.entities.tech.MrpLinesAssortmentEntity;
import huzofw.repository.impl.GenericRepositoryImpl;
import huzofw.repository.impl.MyBatisDaoUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Repository;
import cn.com.huzo.repository.tech.MrpLinesAssortmentRepository;

/**
 * 物料需求计划-搭配类型明细信息数据访问对象。
 *
 * @author 吴旭
 */
@Repository("MrpLinesAssortmentRepository")
public class MrpLinesAssortmentRepositoryImpl extends GenericRepositoryImpl<MrpLinesAssortmentEntity, String>
        implements MrpLinesAssortmentRepository {

    private final String namespace = "cn.com.huzo.repository.tech.MrpLinesAssortmentRepository";

    /**
     * 构造函数,设置the instance to ProductRepositoryImpl.class.
     */
    public MrpLinesAssortmentRepositoryImpl() {
        super(MrpLinesAssortmentEntity.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<MrpLinesAssortmentEntity> fetchByHeaderId(Long headerId) {
        return getSqlSession().selectList(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchByHeaderId"), headerId);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer update(MrpLinesAssortmentEntity instance) {
        return getSqlSession().update(MyBatisDaoUtils.getMapperQueryId(
                namespace, "update"), instance);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer delete(String key) {
        return getSqlSession().delete(MyBatisDaoUtils.getMapperQueryId(
                namespace, "delete"), key);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer batchDelete(List<MrpLinesAssortmentEntity> itemList) {
        return getSqlSession().delete(MyBatisDaoUtils.getMapperQueryId(
                namespace, "batchDelete"), itemList);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer updateByHistoryData(Long headerId, Long relatedHeaderId, Long updatedBy) {
        Map<String, Object> criterion = new HashMap();
        criterion.put("HeaderId", headerId);
        criterion.put("RelatedHeaderId", relatedHeaderId);
        criterion.put("UpdatedBy", updatedBy);

        return getSqlSession().update(MyBatisDaoUtils.getMapperQueryId(
                namespace, "updateByHistoryData"), criterion);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer updateTypeByPriorMrp(Long headerId, Long priorMrpHeaderId, Long bomHeaderId, Long updatedBy) {
        Map<String, Object> criterion = new HashMap();
        criterion.put("HeaderId", headerId);
        criterion.put("PriorMrpHeaderId", priorMrpHeaderId);
        criterion.put("BomHeaderId", bomHeaderId);
        criterion.put("UpdatedBy", updatedBy);

        return getSqlSession().update(MyBatisDaoUtils.getMapperQueryId(
                namespace, "updateTypeByPriorMrp"), criterion);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer insertAssormentSizeByPriorMrp(Long headerId, Long priorMrpHeaderId, Long updatedBy) {
        Map<String, Object> criterion = new HashMap();
        criterion.put("HeaderId", headerId);
        criterion.put("PriorMrpHeaderId", priorMrpHeaderId);
        criterion.put("UpdatedBy", updatedBy);

        return getSqlSession().insert(MyBatisDaoUtils.getMapperQueryId(
                namespace, "insertAssormentSizeByPriorMrp"), criterion);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer cleanPriorMrpAssortmentLinesId(Long headerId) {
        Map<String, Object> criterion = new HashMap();
        criterion.put("HeaderId", headerId);

        return getSqlSession().update(MyBatisDaoUtils.getMapperQueryId(
                namespace, "cleanPriorMrpAssortmentLinesId"), criterion);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer singleAssort(Long headerId, String assortmentLinesId, Long moHeaderId, String styleNo,
            List<Integer> assortmentSize, Short assortmentType, Boolean calByDestinationFlag, List<String> assortmentDestination) {
        Map<String, Object> criterion = new HashMap<>();
        criterion.put("HeaderId", headerId);
        criterion.put("AssortmentLinesId", assortmentLinesId);
        criterion.put("MoHeaderId", moHeaderId);
        criterion.put("StyleNo", styleNo);
        criterion.put("AssortmentSize", assortmentSize);
        criterion.put("AssortmentType", assortmentType);
        criterion.put("CalByDestinationFlag", calByDestinationFlag);
        criterion.put("AssortmentDestination", assortmentDestination);

        return getSqlSession().insert(MyBatisDaoUtils.getMapperQueryId(
                namespace, "singleAssort"), criterion);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer batchAssort(Long headerId, Long moHeaderId, String styleNo, Short assortmentType) {
        Integer rowsAffected = 0;

        Map<String, Object> criterion = new HashMap();
        criterion.put("HeaderId", headerId);
        criterion.put("MoHeaderId", moHeaderId);
        criterion.put("StyleNo", styleNo);

        // assortmentType 0 无搭配 1 颜色搭配 2 尺码搭配 3 颜色+尺码搭配 9 新增材料，计算按照无搭配来进行
        switch (assortmentType) {
            case 0:
                rowsAffected = getSqlSession().insert(MyBatisDaoUtils.getMapperQueryId(
                        namespace, "batchAssort00"), criterion);
                break;
            case 1:
                rowsAffected = getSqlSession().insert(MyBatisDaoUtils.getMapperQueryId(
                        namespace, "batchAssort01"), criterion);
                break;
            case 2:
                rowsAffected = getSqlSession().insert(MyBatisDaoUtils.getMapperQueryId(
                        namespace, "batchAssort02"), criterion);
                break;
            case 3:
                rowsAffected = getSqlSession().insert(MyBatisDaoUtils.getMapperQueryId(
                        namespace, "batchAssort03"), criterion);
                break;
            case 9:
                rowsAffected = getSqlSession().insert(MyBatisDaoUtils.getMapperQueryId(
                        namespace, "batchAssort09"), criterion);
                break;
        }

        return rowsAffected;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer insertLinesForSyncBom(Long headerId, Long bomHeaderId, Long updatedBy) {
        Map<String, Object> criterion = new HashMap<>();
        criterion.put("HeaderId", headerId);
        criterion.put("BomHeaderId", bomHeaderId);
        criterion.put("CreatedBy", updatedBy);
        criterion.put("UpdatedBy", updatedBy);

        return getSqlSession().insert(MyBatisDaoUtils.getMapperQueryId(
                namespace, "insertLinesForSyncBom"), criterion);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer updateTypeForNewLines(Long headerId, Long updatedBy) {
        Map<String, Object> criterion = new HashMap<>();
        criterion.put("HeaderId", headerId);
        criterion.put("UpdatedBy", updatedBy);

        return getSqlSession().update(MyBatisDaoUtils.getMapperQueryId(
                namespace, "updateTypeForNewLines"), criterion);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer deleteLinesForSyncBom(Long headerId, Long bomHeaderId) {
        Map<String, Object> criterion = new HashMap<>();
        criterion.put("HeaderId", headerId);
        criterion.put("BomHeaderId", bomHeaderId);

        return getSqlSession().delete(MyBatisDaoUtils.getMapperQueryId(
                namespace, "deleteLinesForSyncBom"), criterion);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer deleteLinesAssortmentSize(Long headerId, String assortmentLinesId) {
        Map<String, Object> criterion = new HashMap<>();
        criterion.put("HeaderId", headerId);
        criterion.put("AssortmentLinesId", assortmentLinesId);

        return getSqlSession().delete(MyBatisDaoUtils.getMapperQueryId(
                namespace, "deleteLinesAssortmentSize"), criterion);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer insertLinesAssortmentSize(Long headerId, String assortmentLinesId, List<Integer> assortmentSize) {
        Map<String, Object> criterion = new HashMap<>();
        criterion.put("HeaderId", headerId);
        criterion.put("AssortmentLinesId", assortmentLinesId);
        criterion.put("AssortmentSize", assortmentSize);

        return getSqlSession().insert(MyBatisDaoUtils.getMapperQueryId(
                namespace, "insertLinesAssortmentSize"), criterion);
    }
}
